//
//  JYAlertView.m
//  JYEducation-Parent
//
//  Created by RD on 2017/9/19.
//  Copyright © 2017年 com.RDInfo. All rights reserved.
//

#import "JYAlertView.h"
#import "SDAutoLayout.h"

#define MessageFont [UIFont systemFontOfSize:14]

static CGFloat const buttonH = 50.0;
static CGFloat const marginLR = 40.0;
static CGFloat const paddingLR = 15.0;
static CGFloat const paddingUD = 35.0;
static CGFloat const btnPadding = 2;

typedef void(^ActionHandler)(NSInteger tag);

@interface JYAlertView ()

@property (nonatomic, assign) CGFloat contentWidth;

@property (nonatomic, weak) UIView *contentView;
@property (nonatomic, weak) UILabel *titleLabel;
@property (nonatomic, weak) UIView *lineUpView;
@property (nonatomic, weak) UILabel *messageLabel;
@property (nonatomic, weak) UIView *divideLine;
@property (nonatomic, strong) NSArray *titleArr;
@property (nonatomic, strong) NSArray *titleColors;
@property (nonatomic, strong) NSArray<UIButton *> *buttonArray;
@property (nonatomic, strong) NSArray<UIView *> *divideLineArr;
@property (nonatomic, copy) ActionHandler actionHandler;

@end

@implementation JYAlertView

+ (instancetype)showAlertViewWithMessage:(NSString *)message titleArr:(NSArray<NSString *> *)titleArr titleColors:(NSArray<UIColor *> *)titleColors actionHandler:(void (^)(NSInteger))actionHandler {
    return [self showAlertViewWithTitle:nil message:message titleArr:titleArr titleColors:titleColors actionHandler:actionHandler];
}

+ (instancetype)showAlertViewWithTitle:(NSString *)title message:(NSString *)message titleArr:(NSArray<NSString *> *)titleArr titleColors:(NSArray<UIColor *> *)titleColors actionHandler:(void (^)(NSInteger tag))actionHandler {
    NSAssert(titleArr.count<4, @"JYAlertView不支持三个以上按钮");
    CGRect rect = [UIScreen mainScreen].bounds;
    JYAlertView *alertView = [[JYAlertView alloc]initWithFrame:rect];
    alertView.contentWidth = rect.size.width - 2*marginLR;
    alertView.titleLabel.text = title;
    alertView.messageLabel.text = message;
    alertView.titleArr = titleArr;
    alertView.titleColors = titleColors;
    [alertView setButtonsWithCount:titleArr.count];
    alertView.actionHandler = actionHandler;
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:alertView];
    return alertView;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.5];
        [self initUI];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    BOOL showHeaderView = YES;
    if (self.titleLabel.text.length == 0) {
        showHeaderView = NO;
        self.titleLabel.hidden = YES;
    }else {
        self.titleLabel.hidden = NO;
    }
    //标题
    CGFloat titleLabelH = (showHeaderView == YES) ? 40 : 0;
    CGFloat titleLabelX = paddingLR*2;
    CGFloat titleLabelY = 0;
    CGFloat titleLabelW = self.contentWidth - 3*paddingLR;
    
    self.titleLabel.frame = CGRectMake(titleLabelX, titleLabelY, titleLabelW, titleLabelH);
    
    //分割线
    CGFloat lineUpViewX = paddingLR;
    CGFloat lineUpViewY = CGRectGetMaxY(self.titleLabel.frame);
    CGFloat lineUpViewW = self.contentWidth - 2*paddingLR;
    CGFloat lineUpViewH = (showHeaderView == YES) ? 0.5 : 0;
    self.lineUpView.frame = CGRectMake(lineUpViewX, lineUpViewY, lineUpViewW, lineUpViewH);
    
    //内容信息
    CGFloat messageLabelX = paddingLR;
    CGFloat messageLabelY = CGRectGetMaxY(self.lineUpView.frame) + paddingUD;
    CGFloat messageLabelW = self.contentWidth - 2*paddingLR;
    CGSize size = CGSizeMake(messageLabelW, MAXFLOAT);
    NSDictionary *attributes = @{NSFontAttributeName:MessageFont};
    CGSize messageSize = [self.messageLabel.text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
    self.messageLabel.frame = CGRectMake(messageLabelX, messageLabelY, messageLabelW, messageSize.height);
    
    
    CGFloat divideLineX = (showHeaderView == YES) ? paddingLR : 0;
    CGFloat divideLineY = CGRectGetMaxY(self.messageLabel.frame)+paddingUD;
    CGFloat divideLineW = self.contentWidth - 2*divideLineX;
    CGFloat divideLineH = 0.5;
    self.divideLine.frame = CGRectMake(divideLineX, divideLineY, divideLineW, divideLineH);
    
    CGFloat btnW = (self.contentWidth - (self.titleArr.count +1)*btnPadding)/self.titleArr.count;
    CGFloat btnH = buttonH;
    CGFloat btnY = self.divideLine.frame.origin.y + 1;
    for (int i=0; i<self.buttonArray.count; i++) {
        UIButton *button = self.buttonArray[i];
        CGFloat btnX = btnPadding + (btnW +btnPadding)*i;
        button.frame = CGRectMake(btnX, btnY, btnW, btnH);
    }
    CGFloat divideViewW = 0.5;
    CGFloat downLinePadding = (showHeaderView == YES) ? 0 : 5;
    for (int i=0; i<self.divideLineArr.count; i++) {
        UIView *divideView = self.divideLineArr[i];
        UIButton *button = self.buttonArray[i];
        CGFloat lineX = CGRectGetMaxX(button.frame)+1;
        divideView.frame = CGRectMake(lineX, btnY +downLinePadding, divideViewW, btnH -2*downLinePadding);
    }
    
    CGFloat contentViewW = self.contentWidth;
    CGFloat contentViewH = CGRectGetMaxY(self.divideLine.frame) +buttonH;
    CGFloat contentViewX = marginLR;
    CGFloat contentViewY = (self.frame.size.height - contentViewH)/2.0;
    
    self.contentView.frame = CGRectMake(contentViewX, contentViewY, contentViewW, contentViewH);
}

- (void)initUI {
    UIView *contentView = [[UIView alloc]init];
    contentView.backgroundColor = [UIColor whiteColor];
    contentView.layer.cornerRadius = 5;
    contentView.layer.masksToBounds = YES;
    [self addSubview:contentView];
    self.contentView = contentView;
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.font = [UIFont systemFontOfSize:15];
    titleLabel.textColor = [UIColor blackColor];
    [contentView addSubview:titleLabel];
    self.titleLabel = titleLabel;
    
    UIView *lineUpView = [[UIView alloc]init];
    lineUpView.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.5];
    [contentView addSubview:lineUpView];
    self.lineUpView = lineUpView;
    
    UILabel *messageLabel = [[UILabel alloc]init];
    messageLabel.font = MessageFont;
    messageLabel.textColor = RGB(55, 55, 55);
    messageLabel.textAlignment = NSTextAlignmentCenter;
    messageLabel.numberOfLines = 0;
    [contentView addSubview:messageLabel];
    self.messageLabel = messageLabel;
    
    UIView *divideLine = [[UIView alloc]init];
    divideLine.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.5];
    [contentView addSubview:divideLine];
    self.divideLine = divideLine;
}

- (void)setButtonsWithCount:(NSInteger)count {
    NSMutableArray *tempArray = [NSMutableArray array];
    NSMutableArray *tempDivideArr = [NSMutableArray array];
    for (int i=0; i<count; i++) {
        UIColor *btnColor = [UIColor darkGrayColor];
        if (i < self.titleColors.count) {
            btnColor = self.titleColors[i];
        }
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = i;
        [button setTitle:self.titleArr[i] forState:UIControlStateNormal];
        [button setTitleColor:btnColor forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button addTarget:self action:@selector(btnActionClick:) forControlEvents:UIControlEventTouchUpInside];
        [tempArray addObject:button];
        [self.contentView addSubview:button];
        //分割线
        if (i == count -1) break;
        UIView *divideInnerLine = [[UIView alloc]init];
        divideInnerLine.backgroundColor = [UIColor colorWithWhite:0.2 alpha:0.4];
        [tempDivideArr addObject:divideInnerLine];
        [self.contentView addSubview:divideInnerLine];
    }
    self.buttonArray = [tempArray copy];
    self.divideLineArr = [tempDivideArr copy];
}

- (void)setHeaderViewColor:(UIColor *)headerViewColor {
    
    _headerViewColor = headerViewColor;
    self.titleLabel.textColor = headerViewColor;
    self.lineUpView.backgroundColor = headerViewColor;
}

#pragma mark --- btnActionClick
- (void)btnActionClick:(UIButton *)sender {
    if (self.actionHandler) {
        self.actionHandler(sender.tag);
    }
    [UIView animateWithDuration:0.5 animations:^{
        [self removeFromSuperview];
    }];
}

- (void)dealloc {
}

@end
